﻿using DotNetty.Extensions;
using System.Text;

var udp = new UdpSocket(7777);

udp.OnStarting += () =>
{
    Console.WriteLine("Starting...");
};

udp.OnStarted += () =>
{
    Console.WriteLine("Started.");
};

udp.OnStopped += async ex =>
{
    Console.WriteLine($"Stopped==>{ex?.Message}");
    await Task.Delay(2000);
    await udp.StartAsync(); //restart
};

udp.OnException += ex =>
{
    Console.WriteLine($"Exception==>{ex?.Message}");
};

udp.OnMessage += async (endPoint, bytes) =>
{
    Console.WriteLine(endPoint);
    Console.WriteLine(Encoding.UTF8.GetString(bytes));

    var sendBytes = Encoding.UTF8.GetBytes("this is from UDP1 message!");
    await udp.WriteAndFlushAsync(endPoint, sendBytes);
};

await udp.StartAsync();

Console.ReadKey();

await udp.DisposeAsync();

Console.WriteLine("Hello, World!");
